package com.SPOooON.googletaskssync;

import android.content.ContentValues;
import android.database.Cursor;

import com.google.api.client.util.DateTime;
import com.google.api.services.tasks.model.TaskList;

public class TaskListWrapper {
	
	private int _id;
	private boolean newTaskList;
	private TaskList taskList;
	private String account; 
	
	public TaskListWrapper(Cursor c) {
		
		String id = c.getString(c.getColumnIndex(GoogleTasksContentProvider.TaskLists.Columns.ID));
		String eTag = c.getString(c.getColumnIndex(GoogleTasksContentProvider.TaskLists.Columns.ETAG));
		String title = c.getString(c.getColumnIndex(GoogleTasksContentProvider.TaskLists.Columns.TITLE));
		String selfLink = c.getString(c.getColumnIndex(GoogleTasksContentProvider.TaskLists.Columns.SELFLINK));
		
		long updatedLong = c.getLong(c.getColumnIndex(GoogleTasksContentProvider.TaskLists.Columns.UPDATED));
		DateTime updated = updatedLong > 0 ? new DateTime(updatedLong, 0) : null;
		
		_id = c.getInt(c.getColumnIndex(GoogleTasksContentProvider.TaskLists.Columns._ID));
		newTaskList = c.getInt(c.getColumnIndex(GoogleTasksContentProvider.TaskLists.Columns.NEW)) == 0 ? false : true;
		account = c.getString(c.getColumnIndex(GoogleTasksContentProvider.TaskLists.Columns.ACCOUNT));
			
		taskList = new TaskList();
		taskList.setId(id);
		taskList.setEtag(eTag);
		taskList.setTitle(title);
		taskList.setUpdated(updated);
		taskList.setSelfLink(selfLink);
	}
	
	public TaskListWrapper(String account, TaskList taskList) {		
		this.account = account;
		this.taskList = taskList;
		this.newTaskList = false;
	}
	
	public ContentValues getContentValues(){
		
		ContentValues cv = new ContentValues();
		
		cv.put(GoogleTasksContentProvider.TaskLists.Columns.ID, taskList.getId());
		cv.put(GoogleTasksContentProvider.TaskLists.Columns.ETAG, taskList.getEtag());
		cv.put(GoogleTasksContentProvider.TaskLists.Columns.TITLE, taskList.getTitle());
		cv.put(GoogleTasksContentProvider.TaskLists.Columns.UPDATED, (taskList.getUpdated() == null) ? null : taskList.getUpdated().getValue());
		cv.put(GoogleTasksContentProvider.TaskLists.Columns.SELFLINK, taskList.getSelfLink());
		
		cv.put(GoogleTasksContentProvider.TaskLists.Columns.NEW, newTaskList ? 1 : 0);
		cv.put(GoogleTasksContentProvider.TaskLists.Columns.ACCOUNT, account);
		
		return cv;
	}
	
	public TaskList getTaskList() {		
		return taskList;
	}
	
	public void setNew(boolean newTaskList) {
		this.newTaskList = newTaskList;
	}
	
	public boolean getNew(){		
		return this.newTaskList;
	}
	
	public int get_id(){
		return _id;
	}	
}
